#include "StartUpProject.h"

#include "Containers/String.h"
#include "Path/Paths.h"
#include "ScriptCompiler/Compiler.h"
#include "ScriptCompiler/CompileResult.h"
#include "ScriptRuntime/ScriptSubsystem.h"
#include "Output/Console.h"

#include "Asset/AssetSubsystem.h"
#include "Asset/AssetReader.h"

#include "File/FileUtils.h"

void StartUpProject(const SPath& RootFolder )
{
    //load script
    SPath ByteCodeFilePath=RootFolder/U"Build/ByteCode.bin";
    auto ByteCode=SFileUtils::ReadToBinary(ByteCodeFilePath);
    RScriptSubsystem::Get()->LoadByteCode(ByteCode,U"ProjectScript");

    //get project name
    SPath ProjectSettingFilePath;
    for(auto& File : RootFolder.ListFiles())
    {
        if(File.IsFileWithExtension(U".Wuhua"))
        {
            ProjectSettingFilePath=File;
            break;
        }
    }
    if(ProjectSettingFilePath.IsEmpty())
    {
        WH::PrintLine(U"无法找到项目设置文件");
        return;
    }

    CString ProjectName=ProjectSettingFilePath.GetBaseFileName();

    WH::PrintLine(U"运行脚本");

    //run script
    RApplication::GetApplication()->GetSubsystem<RScriptSubsystem>()
        ->InvokeStaticFunction(ProjectName+U"::Entry",U"Main",{});
 
}